/*
 * @(#)KeyGenDAO.java  2010/11/22
 *
 * Copyright (c) 2010. i2finance., Ltd. All rights reserved.
 */

package com.TTBook.utils.keygen.adapter;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import com.TTBook.utils.keygen.SerialInfo;


/**
 * <p>Description:</p>
 *
 * Created Date: 2010-11-22
 * Created time: 14:11:59
 *
 * @author : Chris
 * @version : 
 * @see :
 * @since :   
 *
 */
public class SerialGeneratorDataAdapterImpl extends SqlMapClientDaoSupport implements SerialGeneratorDataAdapter {

    private PlatformTransactionManager txManager;

    private TransactionDefinition txDefinition;

    public void setTxDefinition(TransactionDefinition txDefinition) {
        this.txDefinition = txDefinition;
    }

    public void setTxManager(PlatformTransactionManager txManager) {
        this.txManager = txManager;
    }

    public long getSerialValue(SerialInfo serialInfo) {
        Long serialValue = 0L;
        TransactionStatus txStatus=txManager.getTransaction(txDefinition);
        try {
            getSqlMapClientTemplate().update("serial_no.updateKeyDataValue", serialInfo);
            serialValue = (Long) getSqlMapClientTemplate().queryForObject("serial_no.getKeyDataValue", serialInfo);
            txManager.commit(txStatus);
        } catch (DataAccessException e) {
             txManager.commit(txStatus);
        }
        if (serialValue == null) {
            throw new DataRetrievalFailureException(
                    "serial_no.updateKeyDataValue must retrieva at least one row,but null.");
        }
        return serialValue.longValue();
    }
}
